#light

/// File: VSLabViewlets\viewletexample.fs
/// 
/// Author: Davide Morelli (morellid@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

module VSLabViewlets

open System.Drawing
open System.Windows.Forms
open VSLab


type plotter() =
  inherit Viewlet(Name="a very simple function plotter") as base
  let mutable func = (fun a -> a*a)
  member x.func
    with get() = func
    and set(v) = 
      func <- v
      base.Invalidate()
  override x.OnPaint(e) =
    let offsetX = 10
    let offsetY = 10
    let mutable puntoA:Point = new Point(0,0)
    let mutable puntoB:Point = new Point(0,0)
    let mutable maxX = 400
    let mutable maxY = 400
    if maxX > 400 then
      maxX <- 400
    if maxY > 400 then
      maxY <- 400
    // asse x
    e.Graphics.DrawLine(new Pen(Color.Blue), new Point(offsetX, offsetY), new Point(maxX, offsetY))
    // asse y
    e.Graphics.DrawLine(new Pen(Color.Blue), new Point(offsetX, offsetY), new Point(offsetX, maxY))
    for i = 0 to maxX do
      puntoB <- new Point(offsetX + i, offsetY + func(i))
      if i > 1 then
        e.Graphics.DrawLine(new Pen(Color.Red), puntoA, puntoB)
      puntoA <- puntoB
    done
    
    
// testing the mousemove:
// same as test2 but the offsets follow the mouse
type liner() =
  inherit Viewlet(Name="a segment drawing viewlet") as base
  let mutable func = (fun a -> a*a)
  let mutable p1:Point = new Point(10,10)
  let mutable p2:Point = new Point(100,100)
  let mutable drawing = false
  member x.func
    with get() = func
    and set(v) = 
      func <- v
      base.Invalidate()
  override x.OnMouseMove(e) =
    if drawing then
      p2 <- e.Location
      base.Invalidate()
    //done
    ()
  override x.OnMouseDown(e) =
    p1 <- e.Location
    drawing <- true
    ()
  override x.OnMouseUp(e) = 
    drawing <- false
    ()
  override x.OnPaint(e) =
    // asse x
    e.Graphics.DrawLine(new Pen(Color.Blue), p1, p2)


type clipper() = 
  inherit Viewlet(Name="clipping rect viewlet") as base
  override x.OnPaint(e) =
    // drawing the clip rectangle...
    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width - 2, e.ClipRectangle.Height - 2))
    ()
  override x.OnPaintBackground(e) =
    e.Graphics.FillRectangle(new SolidBrush(Color.Green), new Rectangle(e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height))
    ()
  override x.OnSizeChanged(e) =
    System.Console.WriteLine("onsizechanged")
    base.OnSizeChanged(e)
    ()

// a bouncing ball...

type ball() as x= 
  inherit Viewlet(Name="ball") as base
  let time = new Timer()
  let mutable p = new Point(5,5)
  let mutable velo = 5
  let mutable dirx = velo
  let mutable diry = velo
  do 
    time.Interval <- 25
    time.Tick.Add(x.change)
    time.Enabled <- true
  member x.Velo
    with get() = velo
    and set(v) = velo <- v
  member x.change(e) = 
    if dirx > 0 then
      if p.X > base.Width then
        dirx <- -1
    else
      if p.X < 0 then
        dirx <- 1
    if diry > 0 then
      if p.Y > base.Height then
        diry <- -1
    else
      if p.Y < 0 then
        diry <- 1
    p <- new Point(p.X + dirx*velo, p.Y + diry*velo)
    base.Invalidate() |> ignore
  override x.OnPaint(e)=
    e.Graphics.DrawEllipse(new Pen(Color.Black), p.X - 5, p.Y - 5, 10, 10)


// a viewlet that uses keyboard
type test_key() as x =
  inherit Viewlet(Name="keyboard") as base
  let mutable evento = false
  override x.OnKeyDown(e:KeyEventArgs) =
    if evento then
      evento <- false
    else
      evento <- true
    x.Invalidate()
  override x.OnPaint(e) =
    if evento then
      e.Graphics.DrawEllipse(new Pen(Color.Green), 50, 50, 10, 10)
    else
      e.Graphics.DrawEllipse(new Pen(Color.Red), 50, 50, 10, 10)
    
